package fy.uap.operatorImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.LogFactory;

import com.seeyon.oainterface.exportData.form.ValueExport;

import fy.form.listen.operate.AForm_Save_Operator;
import fy.uap.bean.ERPMergerBean;
import fy.uap.dao.ERPMergerDao;
import fy.util.MyUtil;

public class ERPMergerOperator extends AForm_Save_Operator {

	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(ERPMergerOperator.class);

	private long memberID;
	private List<String> oldName = new ArrayList<String>();
	private List<String> oldCode = new ArrayList<String>();
	private List<String> newName = new ArrayList<String>();
	private List<String> newCode = new ArrayList<String>();
	private List<String> serial = new ArrayList<String>();
	private List<String> oldAddress = new ArrayList<String>();
	private List<String> newAddress = new ArrayList<String>();

	private String number;
	private String createDate;
	private String company;
	private String ou_id;

	public String getFormName() {
		return "供应商合并审批表（ERP）";
	}

	public boolean save() {
		ProviderMessageManager pmm = new ProviderMessageManager();
		pmm.setMemberID(memberID);
		if (oldName.size() > 0 && oldName.size() == oldCode.size()
				&& oldCode.size() == newName.size()
				&& oldCode.size() == newCode.size() && number != null
				&& createDate != null) {

			List<ERPMergerBean> beans = new ArrayList<ERPMergerBean>();
			for (int i = 0, size = oldName.size(); i < size; i++) {
				if (serial.indexOf(serial.get(i)) == i) {
					ERPMergerBean bean = new ERPMergerBean();
					bean.setOldName(oldName.get(i));
					bean.setOldCode(oldCode.get(i));
					bean.setNewCode(newCode.get(i));
					bean.setNewName(newName.get(i));
					bean.setSerial(serial.get(i));
					bean.setNewAddress(newAddress.get(i));
					bean.setOldAddress(oldAddress.get(i));
					beans.add(bean);
					bean = null;
				}
			}

			ERPMergerDao dao = new ERPMergerDao();
			dao.setBeans(beans);
			dao.setCreateDate(createDate);
			dao.setNumber(number);
			dao.setCompany(company);
			dao.setOu_id(ou_id);
			if (dao.save()) {
				return true;
			} else {
				String msg = "【"
						+ super.formBean.getFlowExport().getFlowTitle()
						+ "】写入ERP数据库异常，请联系系统管理员手工导入。";
				pmm.sendMessage(msg);
				log.error(msg);
			}
		} else {
			String msg = "获取【" + super.formBean.getFlowExport().getFlowTitle()
					+ "】的数据异常，无法写入ERP数据库，请联系系统管理员手工导入。";
			pmm.sendMessage(msg);
			log.error(msg);
		}
		return false;
	}

	@Override
	protected void parseOtherField_FirstStep(com.seeyon.v3x.services.flow.bean.FlowExport flowExport) {
		memberID = flowExport.getFlowCreatePerson().getId();

	}

	@Override
	protected void parseValueExport(com.seeyon.v3x.services.form.bean.ValueExport ve) {
		String name = ve.getDisplayName();
		String value = ve.getValue();

		if ("原供应商名称".equals(name)) {
			oldName.add(MyUtil.replaceXMLString(value));
		} else if ("原供应商编码".equals(name)) {
			oldCode.add(MyUtil.replaceXMLString(value));
		} else if ("目标供应商名称".equals(name)) {
			newName.add(MyUtil.replaceXMLString(value));
		} else if ("目标供应商编码".equals(name)) {
			newCode.add(MyUtil.replaceXMLString(value));
		} else if ("序号".equals(name)) {
			serial.add(MyUtil.replaceXMLString(value));
		} else if ("原供应商地点".equals(name)) {
			oldAddress.add(MyUtil.replaceXMLString(value));
		} else if ("目标供应商地点".equals(name)) {
			newAddress.add(MyUtil.replaceXMLString(value));
		} else if ("供应制造部".equals(name)) {
			company = MyUtil.replaceXMLString(value);
		} else if ("OU_ID".equals(name)) {
			ou_id = MyUtil.replaceXMLString(value);
		} else if ("单据编号".equals(name)) {
			number = value;
		} else if ("申请日期".equals(name)) {
			createDate = MyUtil.getDate(value);
		}
	}
}
